package com.company.cloud.uba.modules.jobHandler;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.company.cloud.base.modules.api.models.BizCodeModel;
import com.company.cloud.base.modules.api.service.BaseClientService;
import com.company.cloud.common.bean.BaseClientResult;
import com.company.cloud.common.constant.BaseReturnEnum;
import com.company.cloud.common.redis.service.RedisUtils;
import com.company.cloud.common.utils.CheckUtils;
//import com.company.cloud.uba.modules.app.service.UserOpLogService;
//import com.company.cloud.uba.modules.base.entity.UserBrowseObjLog;
//import com.company.cloud.uba.modules.base.service.UserBrowseObjLogService;
import com.company.cloud.user.modules.api.response.UserSimple;
import com.company.cloud.user.modules.api.service.UserRemoteService;
//import com.xxl.job.core.biz.model.ReturnT;
//import com.xxl.job.core.handler.annotation.XxlJob;
//import com.xxl.job.core.log.XxlJobLogger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.List;

@Component
public class UbaLogTaskJob {
    private static Logger logger = LoggerFactory.getLogger(UbaLogTaskJob.class);

    @Autowired
    private RedisUtils redisUtils;
//    @Autowired
//    private UserBrowseObjLogService userBrowseObjLogService;
//    @Autowired
//    private UserOpLogService userOpLogService;
    @Autowired
    private UserRemoteService userRemoteService;
    @Autowired
    private BaseClientService baseClientService;
    /**
     * uba日志同步
     */
//    @XxlJob("ubaSynHandler")
//    public ReturnT<String> ubaSynHandler(String param) throws Exception {
//        // 获取上次时间戳
//        BizCodeModel bizTimestampModel = new BizCodeModel();
//        bizTimestampModel.setTypeCode("logSyn");
//        bizTimestampModel.setVname("ubaSyn");
//        BaseClientResult<BizCodeModel> resp = baseClientService.getBizCode(bizTimestampModel);
//        if (!BaseReturnEnum.SUCCESS.getCode().equals(resp.getCode())) {
//            XxlJobLogger.log("[uba日志同步-用户关系同步] - 时间戳获取失败");
//            return ReturnT.FAIL;
//        }
//        bizTimestampModel = resp.getData();
//        Long timestamp = Long.parseLong(bizTimestampModel.getVval());
//        List<UserBrowseObjLog> list = userBrowseObjLogService.list(new QueryWrapper<UserBrowseObjLog>()
//                .ge("unix_timestamp(cdt)", timestamp)
//                .orderByAsc("cdt")
//        );
//        if (CollectionUtils.isEmpty(list)) {
//            XxlJobLogger.log("[uba日志同步-用户关系同步] - 无数据");
//            return ReturnT.SUCCESS;
//        }
//        for (UserBrowseObjLog ubaLog : list) {
//            if (CheckUtils.hasInt(ubaLog.getUserId())
//                    && CheckUtils.hasText(ubaLog.getReferralCode())
//                    && !"undefined".equals(ubaLog.getReferralCode())
//            ) {
//                BaseClientResult<UserSimple> userSimpleResp = userRemoteService.getUserByShareCode(ubaLog.getReferralCode());
//                UserSimple userSimple =BaseClientResult.getResultData(userSimpleResp);
//                if (userSimple != null
//                        && CheckUtils.hasInt(userSimple.getId())
//                        && !ubaLog.getUserId().equals(userSimple.getId())
//                ) {
//                    XxlJobLogger.log("[uba日志同步] custUserId:" + ubaLog.getUserId() + ",userId:" + userSimple.getId());
//                    userOpLogService.saveRel(ubaLog.getUserId(), userSimple.getId(), ubaLog.getCip());
//                }
//            }
//            //timestamp = ubaLog.getCdt().toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
//            timestamp = ubaLog.getCdt().toEpochSecond(ZoneOffset.of("+8"));
//        }
//        bizTimestampModel.setVval(Long.toString(timestamp));
//        baseClientService.updateBizCode(bizTimestampModel);
//        XxlJobLogger.log("[uba日志同步-用户关系同步] - 同步成功");
//        return ReturnT.SUCCESS;
//    }

    public void init(){
        logger.info("init");
    }
    public void destroy(){
        logger.info("destory");
    }

}
